package com.atme.micromall.member.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.atme.micromall.member.bean.Members;
import com.atme.micromall.member.quartz.SpringContextUtils;
import com.atme.micromall.member.service.MemberService;
import com.atme.micromall.member.util.KafkaUtils;
import lombok.SneakyThrows;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.PersistJobDataAfterExecution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 定时生日短信  每隔24小时查询今日生日的会员，将该会员手机号发送kafka
 * @author lilinchao
 * @date 2021/9/2
 * @description 1.0
 **/
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Component
public class QuartzJobBirthdateSms extends QuartzJobBean {


    @SneakyThrows
    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext){
        System.out.println("定时生日启动");
        MemberService memberService = SpringContextUtils.getBean(MemberService.class);
        KafkaUtils kafkaUtils = SpringContextUtils.getBean(KafkaUtils.class);

        List<String> phoneList=new ArrayList<>();
        List<Members> membersList= memberService.getAllMembers();
        for (Members members:membersList){

            Date date=members.getBirthdate();
            System.out.println("生日："+date);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String s = sdf.format(new Date());
            Date dateNow =  sdf.parse(s);
            System.out.println("当前日期"+dateNow);

            //日期比较  相同返回0，不同返回-1
            int compareTo = date.compareTo(dateNow);
            if(compareTo==0){
                phoneList.add(members.getPhoneNumber());
            }
        }

        //将封装手机号的List集合转为json字符串
        String jsonString = JSON.toJSONString(phoneList);
        System.out.println(jsonString);

        //将字符串发给kafka
        kafkaUtils.sendMessage("BirthdatePhoneTopic",jsonString);

/*        List<String> strings= JSON.parseObject(jsonString,new TypeReference<List<String>>(){});
        System.out.println(strings);*/


    }
}
